package scr.hotel;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import scr.hibernate.InitSessionFactory;

public class Tempo {
	// ate o ultimo uso do programa
	private int id;
	private Date diaBase;
		
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public Date getDiaBase() {
		return diaBase;
	}
	public void setDiaBase(Date diaBase) {
		this.diaBase = diaBase;
	}
	/*	public long getTempoInicio() {
		return tempoInicio;
	}
	public void setTempoInicio(long tempoInicio) {
		this.tempoInicio = tempoInicio;
	}
*/
	public static void desligando(Tempo tempo, Date data)
	{
		tempo.setDiaBase( data );
	}


	public static boolean saveTempo(Tempo tempo) {
		Transaction tx = null;
	
		Session session = InitSessionFactory.getInstance().getCurrentSession();

		try {
			tx = session.beginTransaction();
			session.save(tempo);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		}

		return true;
	}
	
	public static Date getTempo(){
		Transaction tx = null;
		Session sessao = InitSessionFactory.getInstance().getCurrentSession();
		List lista = null;
		Tempo tempo = null;
		try {
			tx = sessao.beginTransaction();
			lista = sessao.createCriteria(Tempo.class)
			.list();
			
			tx.commit();
			
			if( lista.isEmpty() ) return null;
			tempo = (Tempo)lista.get(0);
	
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return tempo.getDiaBase();
	}
	
	public static int getDaysPassedSince(Date dateLastModified) {

        GregorianCalendar now = new GregorianCalendar();
        
		System.out.println("DIA: " + now.DAY_OF_WEEK_IN_MONTH);
		System.out.println("MES: " + now.MONTH);
		System.out.println("ANO: " + now.YEAR);

        
        GregorianCalendar lastModified = (GregorianCalendar)now.clone();
        lastModified.setTimeInMillis(dateLastModified.getTime());
        return now.get(Calendar.DAY_OF_YEAR)
            - lastModified.get(Calendar.DAY_OF_YEAR)
            + (now.get(Calendar.YEAR) - lastModified.get(Calendar.YEAR))
            * 365;
    }
}
